/**
 * 
 */
package registnet.courseDemand.extras;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Dexter Ryan Carrasco
 */
public class UnderloadedComputer2 {
	public static void main(final String args[]) {
		final File cutFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/CUT_WRITEIN_DATA.csv");
		final File scaFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/SCA_2008_limited_STUD_SUBJ_OUTPUT.csv");
		final File snaFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/SNA_2008_limited_STUD_SUBJ_OUTPUT.csv");
		final File underFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/underloadedReport_2008_limited.csv");
		final Map<String, StudentRec> students = new HashMap<String, StudentRec>();
		final Map<String, StudentRec> SCA = new HashMap<String, StudentRec>();
		final Map<String, StudentRec> SNA = new HashMap<String, StudentRec>();

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedReader input = new BufferedReader(new FileReader(
					cutFile));
			// use buffering

			try {
				String line = null; // not declared within while loop
				String tokens[];

				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {
					tokens = line.split(",");

					if (students.containsKey(tokens[0])) {
						final StudentRec student = students.get(tokens[0]);
						student.addCourse(tokens[1], Integer
								.parseInt(tokens[2]));
					} else {
						final StudentRec student = new StudentRec(tokens[0]);
						student.addCourse(tokens[1], Integer
								.parseInt(tokens[2]));

						students.put(tokens[0], student);
					}

				}
			} finally {
				input.close();

			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedReader input = new BufferedReader(new FileReader(
					scaFile));
			// use buffering

			try {
				String line = null; // not declared within while loop
				String tokens[];

				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				input.readLine();
				while ((line = input.readLine()) != null) {
					tokens = line.split(",");

					if (SCA.containsKey(tokens[0])) {
						final StudentRec student = SCA.get(tokens[0]);
						student.addCourse(tokens[1], students.get(tokens[0])
								.getCourses().get(tokens[1]));
					} else {
						final StudentRec student = new StudentRec(tokens[0]);
						student.addCourse(tokens[1], students.get(tokens[0])
								.getCourses().get(tokens[1]));

						SCA.put(tokens[0], student);
					}

				}
			} finally {
				input.close();

			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedReader input = new BufferedReader(new FileReader(
					snaFile));
			// use buffering

			try {
				String line = null; // not declared within while loop
				String tokens[];

				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				input.readLine();
				while ((line = input.readLine()) != null) {
					tokens = line.split(",");

					if (SNA.containsKey(tokens[0])) {
						final StudentRec student = SNA.get(tokens[0]);
						student.addCourse(tokens[1], students.get(tokens[0])
								.getCourses().get(tokens[1]));
					} else {
						final StudentRec student = new StudentRec(tokens[0]);
						student.addCourse(tokens[1], students.get(tokens[0])
								.getCourses().get(tokens[1]));

						SNA.put(tokens[0], student);
					}

				}
			} finally {
				input.close();

			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedWriter output = new BufferedWriter(new FileWriter(
					underFile));
			// use buffering

			try {
				output
						.write("Student No.,Rec Units,No. Rec Courses,SCA Units,SCA Lack Units,SCA % Units Completed,SCA no. of courses,SCA lack courses,SCA % course completed,SNA Units,SNA Lack Units,SNA % Units Completed,SNA no. of courses,SNA lack courses,SNA % course completed");
				output.newLine();
				for (final Iterator<String> stIter = students.keySet()
						.iterator(); stIter.hasNext();) {
					final String studentNumber = stIter.next();
					final StudentRec student = students.get(studentNumber);
					final int recommended = student.getTotalUnits();
					final int recCourses = student.getCourses().size();
					int scaUnits;
					int scaCourses;
					int snaUnits;
					int snaCourses;

					final StudentRec SCAstudent = SCA.get(studentNumber);
					if (SCAstudent == null) {
						scaUnits = 0;
						scaCourses = 0;
					} else {
						scaUnits = SCAstudent.getTotalUnits();
						scaCourses = SCAstudent.getCourses().size();
					}

					final StudentRec SNAstudent = SNA.get(studentNumber);
					if (SNAstudent == null) {
						snaUnits = 0;
						snaCourses = 0;
					} else {
						snaUnits = SNAstudent.getTotalUnits();
						snaCourses = SNAstudent.getCourses().size();
					}

					final StringBuffer sb = new StringBuffer();
					sb.append(studentNumber);
					sb.append(',');
					sb.append(recommended);
					sb.append(',');
					sb.append(recCourses);
					sb.append(',');

					sb.append(scaUnits);
					sb.append(',');
					sb.append(recommended - scaUnits);
					sb.append(',');
					if (recommended != 0) {
						sb.append((double) scaUnits / recommended * 100);
					} else {
						if (scaUnits == recommended) {
							sb.append(100);
						} else {
							sb.append(-1);
						}
					}
					sb.append(',');
					sb.append(scaCourses);
					sb.append(',');
					sb.append(recCourses - scaCourses);
					sb.append(',');
					if (recCourses != 0) {
						sb.append((double) scaCourses / recCourses * 100);
					} else {
						if (scaCourses == recCourses) {
							sb.append(100);
						} else {
							sb.append(-1);
						}
					}
					sb.append(',');

					sb.append(snaUnits);
					sb.append(',');
					sb.append(recommended - snaUnits);
					sb.append(',');
					if (recommended != 0) {
						sb.append((double) snaUnits / recommended * 100);
					} else {
						if (snaUnits == recommended) {
							sb.append(100);
						} else {
							sb.append(-1);
						}
					}
					sb.append(',');
					sb.append(snaCourses);
					sb.append(',');
					sb.append(recCourses - snaCourses);
					sb.append(',');
					if (recCourses != 0) {
						sb.append((double) snaCourses / recCourses * 100);
					} else {
						if (snaCourses == recCourses) {
							sb.append(100);
						} else {
							sb.append(-1);
						}
					}

					output.write(sb.toString());
					output.newLine();
				}

			} finally {
				output.close();

			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}
	}
}
